package algebra.rewriting.transform;

import algebra.op.AlgebraicOP;
import algebra.op.ScanOP;
import algebra.op.SelectionOP;
import algebra.rewriting.RewriteRule;

public class ScanSelection extends RewriteRule {

	private final AlgebraicOP op;

	public ScanSelection(AlgebraicOP op) {
		this.op = op;
	}

	@Override
	public AlgebraicOP rewrite() {
		ScanOP sp = op.asScanOP();
		SelectionOP lp = sp.getSubquery().asSelectionOP();
		return new SelectionOP(new ScanOP(lp.getSubquery(),
				sp.getSecondaryIndex()), lp.getSelectionCondition(),
				lp.getPlanEvalKB());
	}

	@Override
	public boolean applicable() {
		if (op instanceof ScanOP) {
			if (op.asScanOP().getSubquery() instanceof SelectionOP) {
				return true;
			}
		}
		return false;
	}

}
